package com.imooc.spring.jdbc.dao;

import com.imooc.spring.jdbc.entity.Employee;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class EmployeeDao {
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public Employee findById(Integer eno) {
        String sql = "select * from  employee where  eno=?";
        Employee employee = jdbcTemplate.queryForObject(sql, new Object[]{eno}, new BeanPropertyRowMapper<Employee>(Employee.class));
        return employee;
    }

    public List<Employee> findByDename(String dename) {
        String sql = " select * from employee where dename =?";
        List<Employee> list = jdbcTemplate.query(sql, new Object[]{dename}, new BeanPropertyRowMapper<Employee>(Employee.class));
        return list;

    }

    public int delete(Integer eno) {
        String sql = "delete from employee where eno =? ";
        int count = jdbcTemplate.update(sql, new Object[]{eno});
        return count;
    }

    public int update(Employee employee){
        String sql="update employee set ename =? ,salary=?,dename=?, hiredate=? where eno=?";
        int count = jdbcTemplate.update(sql, new Object[]{employee.getEname(), employee.getSalary(), employee.getDename(), employee.getHiredate(),employee.getEno()});
        return count;
    }

    public int insert(Employee employee){
        String sql="insert into employee (eno,ename,salary,dename,hiredate) values (?,?,?,?,?)";
        int count = jdbcTemplate.update(sql, new Object[]{employee.getEno(), employee.getEname(), employee.getSalary(), employee.getDename(), employee.getHiredate()});
        return count ;

    }

}
